package com.sx.zongzhi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.ImagePlayActivity;
import com.sx.zongzhi.bean.HouseBean;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.utils.BaseUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 房屋基本信息
 */
public class HouseBasicInforFragment extends Fragment {
    @BindView(R.id.imv_Head)
    ImageView imvHead;
    @BindView(R.id.tv_addressType)
    TextView tvAddressType;
    @BindView(R.id.et_address)
    TextView etAddress;
    @BindView(R.id.tv_houseUses)
    TextView tvHouseUses;
    @BindView(R.id.tv_houseArea)
    TextView tvHouseArea;
    @BindView(R.id.tv_certificateType)
    TextView tvCertificateType;
    @BindView(R.id.et_certificateNumbe)
    TextView etCertificateNumbe;
    @BindView(R.id.et_name)
    TextView etName;
    @BindView(R.id.tv_propertyPersonMobile)
    TextView tvPropertyPersonMobile;
    @BindView(R.id.tv_propertyPersonTel)
    TextView tvPropertyPersonTel;
    @BindView(R.id.et_nowLiveAddress)
    TextView etNowLiveAddress;
    @BindView(R.id.tv_isRentalHouse)
    TextView tvIsRentalHouse;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.et_community)
    TextView etCommunity;
    @BindView(R.id.rl_community)
    RelativeLayout rlCommunity;
    @BindView(R.id.et_block)
    TextView etBlock;
    @BindView(R.id.et_unit)
    TextView etUnit;
    @BindView(R.id.et_room)
    TextView etRoom;
    @BindView(R.id.rl_block)
    LinearLayout rlBlock;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_house_basic_infor, container, false);
        ButterKnife.bind(this, view);
        HouseBean houseBean = (HouseBean) getArguments().getSerializable("houseBean");
        initData(houseBean);
        return view;
    }

    public void initData(HouseBean houseBean) {
        if (houseBean == null){
            return;
        }
        if (!BaseUtil.isEmpty(houseBean.getImgUrl())) {
            final String path=ServiceFactory.NEW_API_BASE_URL + File.separator + houseBean.getImgUrl();
            RequestOptions options = new RequestOptions().centerCrop()
                    .placeholder(R.mipmap.img_deful_house)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .error(R.mipmap.img_deful_house);
            Glide.with(getActivity())
                    .load(path)
                    .apply(options)
                    .into(imvHead);
            imvHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), ImagePlayActivity.class)
                            .putExtra("path",path));
                }
            });
        }
        String addressType = houseBean.getAddressType() == null ? "" : houseBean.getAddressType().getDisplayName();
        tvAddressType.setText(addressType);
        if (addressType.equals("商品房")){
            llAddress.setVisibility(View.GONE);
            rlBlock.setVisibility(View.VISIBLE);
            rlCommunity.setVisibility(View.VISIBLE);
            etCommunity.setText(BaseUtil.isEmpty(houseBean.getCommunity()) ? "" : houseBean.getCommunity());
            etBlock.setText(BaseUtil.isEmpty(houseBean.getBlock()) ? "" : houseBean.getBlock());
            etUnit.setText(BaseUtil.isEmpty(houseBean.getUnit()) ? "" : houseBean.getUnit());
            etRoom.setText(BaseUtil.isEmpty(houseBean.getRoom()) ? "" : houseBean.getRoom());
        }else {
            rlBlock.setVisibility(View.GONE);
            rlCommunity.setVisibility(View.GONE);
            llAddress.setVisibility(View.VISIBLE);
            etAddress.setText(BaseUtil.isEmpty(houseBean.getAddress()) ? "" : houseBean.getAddress());
        }
        tvHouseUses.setText(houseBean.getHouseUses() == null ? "" : houseBean.getHouseUses().getDisplayName());
        tvHouseArea.setText(houseBean.getHouseArea() + "");
        tvCertificateType.setText(houseBean.getCertificateType() == null ? "" : houseBean.getCertificateType().getDisplayName());
        etCertificateNumbe.setText(BaseUtil.isEmpty(houseBean.getCertificateNumbe()) ? "" : houseBean.getCertificateNumbe());
        etName.setText(BaseUtil.isEmpty(houseBean.getName()) ? "" : houseBean.getName());
        tvPropertyPersonMobile.setText(BaseUtil.isEmpty(houseBean.getPropertyPersonMobile()) ? "" : houseBean.getPropertyPersonMobile());
        tvPropertyPersonTel.setText(BaseUtil.isEmpty(houseBean.getPropertyPersonTel()) ? "" : houseBean.getPropertyPersonTel());
        etNowLiveAddress.setText(BaseUtil.isEmpty(houseBean.getNowLiveAddress()) ? "" : houseBean.getNowLiveAddress());
        tvIsRentalHouse.setText(houseBean.isRentalHouse() ? "是" : "否");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
